package utils

import (
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"web_end/global"
	"web_end/proto"
)

// CreateUserServerGrpc /* 创建用户grpc客户端
func CreateUserServerGrpc(apiName string, serverName string) *proto.UserClient {
	ip := global.ServerConfig.UserSrvInfo.Host
	port := global.ServerConfig.UserSrvInfo.Port
	// 启动grpc连接
	userConnect, err := grpc.Dial(fmt.Sprintf("%s:%d", ip, port), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		msg := fmt.Sprintf("[%s] - [%s]", apiName, serverName)
		zap.S().Errorw(msg, "msg", err.Error())
	}

	// 生成grpc的client并调用接口
	userServerClient := proto.NewUserClient(userConnect)

	return &userServerClient
}

// CreateProjectGrpc /* 创建项目grpc客户端
func CreateProjectGrpc(apiName string, serverName string) *proto.ProjectClient {
	port := global.ServerConfig.ProjectSrvInfo.Port
	ip := global.ServerConfig.ProjectSrvInfo.Host
	// 启动grpc连接
	userConnect, err := grpc.Dial(fmt.Sprintf("%s:%d", ip, port), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		msg := fmt.Sprintf("[%s] - [%s]", apiName, serverName)
		zap.S().Errorw(msg, "msg", err.Error())
	}

	projectServerClient := proto.NewProjectClient(userConnect)
	return &projectServerClient
}
